package com.situ.hospital.patient.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.situ.hospital.doctor.model.DoctorModel;
import com.situ.hospital.doctor.service.DoctorService;
import com.situ.hospital.patient.model.PatientModel;
import com.situ.hospital.patient.service.PatientService;
import com.situ.hospital.room.model.RoomModel;
import com.situ.hospital.room.service.RoomService;

import util.IsEmpty;

/**
 * Servlet implementation class PatientServlet
 */
@WebServlet("/PatientServlet")
public class PatientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private PatientService service = new PatientService();

	private DoctorService ds = new DoctorService();
	private RoomService rs = new RoomService();

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");

		switch (action) {
		case "selectdocroom":
			selectdocroom(request, response);
			break;
		case "add":
			add(request, response);
			break;
		case "page":
			page(request, response);
			break;
		case "del":
			del(request,response);
		case "selectmodel":
			selectmodel(request, response);
			break;
		case "upd":
			upd(request, response);
			break;
		}
	}

	private void upd(HttpServletRequest request, HttpServletResponse response) {
		String name=request.getParameter("patientname");
		String sex=request.getParameter("patientsex");
		System.out.println(name+"0"+sex);
		if (!IsEmpty.isEmpty(name) && !IsEmpty.isEmpty(sex)) {
			PatientModel patient=new PatientModel();
			patient.setPatientName(name);
			patient.setPatientSex(sex);
			
			try {
				response.getWriter().write(String.valueOf(service.update(patient)));
			} catch (IOException | SQLException e) {
				e.printStackTrace();
			}
		}
		
	}

	private void selectmodel(HttpServletRequest request, HttpServletResponse response) {
		String patientname=request.getParameter("updname");
		PatientModel patient=new PatientModel();
		patient.setPatientName(patientname);
		try {
			JSONObject jso = new JSONObject(service.selectModel(patient));
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
		
	}

	private void del(HttpServletRequest request, HttpServletResponse response) {
		String delname = request.getParameter("delname");
		PatientModel patient=new PatientModel();
		patient.setPatientName(delname);
		try {
			response.getWriter().write(String.valueOf(service.del(patient)));
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
		
	}

	private void page(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		
		String name = request.getParameter("patientname");
		String sex = request.getParameter("patientsex");
//		String doctorname = request.getParameter("doctorname");
//		String roomaddress = request.getParameter("roomaddress");
		String limit = request.getParameter("limit");
		String curr = request.getParameter("curr");
//		System.out.println(name);
		DoctorService ds=new DoctorService();
		RoomService rs=new RoomService();
		
		
		PatientModel patient=new PatientModel();
		patient.setPatientName(name);
		patient.setPatientSex(sex);
		patient.setPageLimit(Integer.parseInt(limit));
		patient.setPageIndex(Integer.parseInt(curr));
		patient.setPageOn(true);
		
		
		try {
			List<DoctorModel> doctorlist=ds.select(new DoctorModel());
			List<RoomModel> roomlist=rs.select(new RoomModel());
			map.put("doctorlist", doctorlist);
			map.put("roomlist", roomlist);
			map.put("list", service.select(patient));
			map.put("count", service.selectCount(patient));
			JSONObject jso = new JSONObject(map);
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}

		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		String name = request.getParameter("patientname");
		String sex = request.getParameter("patientsex");
		String doctorname = request.getParameter("doctorname");
		String roomaddress = request.getParameter("roomaddress");

		RoomModel room = new RoomModel();
		room.setRoomAddress(roomaddress);

		DoctorModel doctor = new DoctorModel();
		doctor.setDoctorName(doctorname);

//		System.out.println(room.getRoomAddress());
		try {
			room = (RoomModel) rs.selectModel(room).get("selectmodel");
			doctor = (DoctorModel) ds.selectModel(doctor).get("selectmodel");
			PatientModel patient = new PatientModel(name, sex, room.getRoomId(), doctor.getDoctorId());
			response.getWriter().write(String.valueOf(service.add(patient)));
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}

	}

	private void selectdocroom(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<>();
		try {
			map.put("doctorlist", ds.select(new DoctorModel()));
			map.put("roomlist", rs.select(new RoomModel()));
			JSONObject jso = new JSONObject(map);
//			System.out.println(jso.toString());
			response.getWriter().print(jso);
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}

	}

}
